//
//  LabelUtil.swift
//  Extend
//
//  Created by 郑浩 on 2017/9/21.
//  Copyright © 2017年 敬信. All rights reserved.
//

import UIKit

public extension UILabel {
    
    /// 改变label部分文字字体
    ///
    /// - Parameters:
    ///   - partText: 文字
    ///   - fontSize: 字体大小
    ///   - fontName: 字体样式
    ///   - label: label
    public class func changePartTextFont(_ label : UILabel, _ partText: String, _ fontSzie: CGFloat, _ fontName: String) {
        let allText = NSMutableAttributedString(string: label.text!)
        let range = NSRange(location: (allText.string as NSString).range(of: partText).location, length: (allText.string as NSString).range(of: partText).length)
        
        let font = UIFont.init(name: fontName, size: fontSzie)
        allText.addAttribute(NSFontAttributeName, value: font, range: range)
        label.attributedText = allText
    }
    
    /// 改变label部分文字字体颜色
    ///
    /// - Parameters:
    ///   - partText: 部分文字
    ///      - labelText: 文字
    ///   - fontColor: 字体颜色
    ///   - label: label
    public class func changePartTextColor(_ label : UILabel,_ labelText: String, _ partText: String, _ fontColor: UIColor) {
        let string = labelText
        let ranStr = partText
        let attrstring:NSMutableAttributedString = NSMutableAttributedString(string:string)
        let str = NSString(string: string)
        let theRange = str.range(of: ranStr)
        attrstring.addAttribute(NSForegroundColorAttributeName, value: fontColor, range: theRange)
        label.attributedText = attrstring
    }
    
    /// 改变label部分文字字体颜色和字体
    ///
    /// - Parameters:
    ///   - partText: 部分文字
    ///      - labelText: 文字
    ///   - fontColor: 字体颜色
    ///   - fontSize: 字体大小
    ///   - fontName: 字体样式
    ///   - label: label
    public class func changePartText(_ label : UILabel,_ labelText: String, _ partText: String, _ fontColor: UIColor, _ fontSzie: CGFloat, _ fontName: String) {
        let string = labelText
        let ranStr = partText
        let attrstring:NSMutableAttributedString = NSMutableAttributedString(string:string)
        let str = NSString(string: string)
        let theRange = str.range(of: ranStr)
        
        if fontName.count != 0{
            let font = UIFont.init(name: fontName, size: fontSzie)
            attrstring.addAttribute(NSFontAttributeName, value: font, range: theRange)
        }
        attrstring.addAttribute(NSForegroundColorAttributeName, value: fontColor, range: theRange)
        label.attributedText = attrstring
    }
    
    public class func changePartTextColors(_ label : UILabel,_ labelText: String, _ partText: String, _ fontColor: UIColor, _ fontName: String, _ fontSize: CGFloat) {
        let string = labelText
        let ranStr = partText
        let attrstring:NSMutableAttributedString = NSMutableAttributedString(string:string)
        let str = NSString(string: string)
        let theRange = str.range(of: ranStr)
        attrstring.addAttribute(NSForegroundColorAttributeName, value: fontColor, range: theRange)
        attrstring.addAttribute(NSFontAttributeName, value:UIFont.init(name: fontName, size: fontSize)!, range: theRange)
        label.attributedText = attrstring
    }
    
    public class func getLabHeight(labelStr: String, font: UIFont, width: CGFloat) -> CGFloat {
        let statusLabelText: NSString = labelStr as NSString
        let size =  CGSize(width: width, height: 900)
        let dic = NSDictionary(object: font, forKey: NSFontAttributeName as NSCopying)
        let strSize = statusLabelText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? [String : AnyObject], context: nil).size
        return strSize.height
    }
    
    public func setImage(frame: CGRect, image: UIImage) {
        //创建富文本
        var attri = NSMutableAttributedString(string: self.text!)
        //NSTextAttachment可以将要插入的图片作为特殊字符处理
        let attch: NSTextAttachment? = NSTextAttachment()
        //定义图片内容及位置和大小
        attch?.image = image
        attch?.bounds = frame
        //创建带有图片的富文本
        var string = NSAttributedString(attachment: attch!)
        
        //将图片放在第一位
        attri.insert(string, at: 0)
        //用label的attributedText属性来使用富文本
        self.attributedText = attri;
    }
    
    /// 按钮渐变
    ///
    /// - Parameters:
    ///   - original: 起始颜色
    ///   - new: 终点颜色
    ///   - type: 方向 0：从上到下 1：从左到右
    func createGradient(_ colors: Array<UIColor>, _ type: Int) {
        let image = UIImage.setGradientImageWithBounds(rect: self.bounds, colors: colors, type: type)
        self.backgroundColor = UIColor(patternImage: image)
    }
}


